package com.haixiaoke.saas.enterprise.department.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.constant.TenantConstants;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.department.domain.Tenant;
import com.haixiaoke.saas.department.service.ITenantService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 机构信息Controller
 *
 * @author xiaolong
 * @date 2023-12-26
 */
@RestController
@RequestMapping("/enterprise/department/department")
public class EnterpriseTenantController extends BaseController {
    @Autowired
    private ITenantService departmentService;

    /**
     * 查询机构信息列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:tenant:list')")
    @GetMapping("/list")
    public TableDataInfo list(Tenant department) {
        startPage();
        List<Tenant> list = departmentService.selectDepartmentList(department);
        return getDataTable(list);
    }

    /**
     * 导出机构信息列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:tenant:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Tenant department) {
        List<Tenant> list = departmentService.selectDepartmentList(department);
        ExcelUtil<Tenant> util = new ExcelUtil<Tenant>(Tenant.class);
        util.exportExcel(response, list, "机构信息数据");
    }

    /**
     * 获取机构信息详细信息
     */
    @GetMapping
    @TenantScope
    public AjaxResult getInfo() {
        return success(departmentService.selectDepartmentByDepId(TenantContext.getTenantId()));
    }

    /**
     * 获取机构信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:tenant:query')")
    @GetMapping("/info")
    @TenantScope
    public AjaxResult getDepartmentInfo() {
        return success(departmentService.selectDepartmentByDepId(TenantContext.getTenantId()));
    }

    /**
     * 新增机构信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:tenant:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Tenant department) {
        return toAjax(departmentService.insertDepartment(department));
    }

    /**
     * 修改机构信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:tenant:edit')")
    @PutMapping
    @TenantScope
    public AjaxResult edit(Tenant department, @RequestParam(value = "file", required = false) MultipartFile file) {
        return toAjax(departmentService.updateDepartment(department, file));
    }

    /**
     * 删除机构信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:tenant:remove')")
    @DeleteMapping("/{tenantIds}")
    public AjaxResult remove(@PathVariable String[] tenantIds) {
        return toAjax(departmentService.deleteDepartmentByDepIds(tenantIds));
    }


    /**
     * 获取电子签信息
     */
    @GetMapping("/electronicVisaInfo")
    @TenantScope
    public AjaxResult getElectronicVisaInfo() {
        return success(departmentService.getElectronicVisaInfo());
    }

    /**
     * 修改机构派单状态
     */
    @PreAuthorize("@ss.hasPermi('enterprise:tenant:edit')")
    @PutMapping("/orderAllocationState")
    @TenantScope
    public AjaxResult updateOrderAllocationState() {
        Tenant tenant = departmentService.selectDepByDepId(TenantContext.getTenantId());
        if (StrUtil.equals(TenantConstants.ORDER_ALLOCATION_STATE_YES, tenant.getOrderAllocationState())) {
            tenant.setOrderAllocationState(TenantConstants.ORDER_ALLOCATION_STATE_NO);
        } else {
            tenant.setOrderAllocationState(TenantConstants.ORDER_ALLOCATION_STATE_YES);
        }
        return toAjax(departmentService.updateTenant(tenant));
    }
}
